package com.iot.show.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.aliyun.alink.linkkit.api.LinkKit;
import com.aliyun.alink.linksdk.cmp.connect.channel.MqttPublishRequest;
import com.aliyun.alink.linksdk.cmp.core.base.AMessage;
import com.aliyun.alink.linksdk.cmp.core.base.ARequest;
import com.aliyun.alink.linksdk.cmp.core.base.AResponse;
import com.aliyun.alink.linksdk.cmp.core.base.ConnectState;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectNotifyListener;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSendListener;
import com.aliyun.alink.linksdk.tools.AError;
import com.iot.show.R;
import com.iot.show.model.MessageModel;
import com.iot.show.utils.GsonUtil;
import com.iot.show.utils.T;

import java.util.HashMap;
import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {


    private Context mContext;

    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        mContext = this;
        initView();
        // 注册下行监听，包括长连接的状态和云端下行的数据
        LinkKit.getInstance().registerOnPushListener(notifyListener);
    }

    private void initView() {

    }

    @Override
    protected void onDestroy() {
        // 取消下行监听，需要确保和注册的是同一个对象
        LinkKit.getInstance().unRegisterOnPushListener(notifyListener);
        super.onDestroy();
    }


    void loadData(MessageModel model) {

    }

    IConnectNotifyListener notifyListener = new IConnectNotifyListener() {
        @Override
        public void onNotify(String connectId, String topic, AMessage aMessage) {
            // 云端下行数据回调
            // connectId 连接类型 topic 下行 topic; aMessage 下行数据
            // 数据解析如下：
            // String pushData = new String((byte[]) aMessage.data);
            // pushData 示例  {"method":"thing.service.test_service","id":"123374967","params":{"vv":60},"version":"1.0.0"}
            // method 服务类型； params 下推数据内容
            String json = new String((byte[]) aMessage.data);
            Log.e("TAG", "onNotify: "+json );
            HashMap m = JSON.parseObject(json, LinkedHashMap.class, Feature.OrderedField);
            MessageModel messageModel = GsonUtil.getGson().fromJson(new String((byte[]) aMessage.data), MessageModel.class);
            if (messageModel != null) {
                loadData(messageModel);
            } else {
                T.info("Topic : " + topic + " 数据为空！");
            }

        }

        @Override
        public boolean shouldHandle(String connectId, String topic) {
            // 选择是否不处理某个 topic 的下行数据
            // 如果不处理某个topic，则onNotify不会收到对应topic的下行数据
            return true; //TODO 根基实际情况设置
        }

        @Override
        public void onConnectStateChange(String connectId, ConnectState connectState) {
            // 对应连接类型的连接状态变化回调，具体连接状态参考 SDK ConnectState
            if (connectState.equals(ConnectState.CONNECTED)) {
                T.success("连接服务器成功");
            }
            if (connectState.equals(ConnectState.DISCONNECTED)) {
                T.error("服务器连接断开");
            }

        }
    };


}